package com.adams.action.audit;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import com.adams.access.db.bean.AuditRecord;
import com.adams.action.view.ViewAction;
import com.adams.audit.operate.service.IAuditRecordService;
import com.adams.common.BaseAction;
import com.adams.core.exception.BusinessException;
import com.adams.custom.page.mgt.service.ViewParseService;
import com.adams.custom.page.util.ViewConstant;
import com.adams.custom.page.util.ViewUtil;
import com.adams.encache.mechanism.CacheUtil;
import com.adams.info.AdamsConstant;
import com.adams.utils.SpringBeanFactoryUtil;
import com.sun.xml.registry.uddi.bindings_v2_2.BusinessService;

public class AuditRecordAction extends BaseAction {

	private static final long serialVersionUID = -4376794838373575979L;

	private ViewParseService viewParseService;

	private IAuditRecordService auditRecordService;

	private BusinessService businessService;

	private int menuId;

	private String gridPanelId;

	private String viewEventId;

	private String searchPanelId;

	/**
	 * 
	 * Description : 显示布局
	 * 
	 * @return String
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String listPanel() throws Exception {
		Map map = viewParseService.getParseViewByMenuId(getUserId(), menuId);
		if (((List) map.get("wins")).size() >= 2) {
			Map formMap = (Map) ((Map) ((List) map.get("wins")).get(0))
					.get("formPanel");
			Map gridMap = (Map) ((Map) ((List) map.get("wins")).get(1))
					.get("gridPanel");
			String panelId = formMap.get("id").toString();
			String storeUrl = (String) gridMap.get("storeUrl");
			storeUrl = ViewUtil.addParameterToUrl(storeUrl, "searchPanelId",
					panelId);
			gridMap.put("storeUrl", storeUrl);
		}

		write(JSONObject.fromObject(map).toString());
		return null;
	}

	/**
	 * Description : 显示查询条字段信息
	 * 
	 * @return
	 * @throws Exception
	 * 
	 */
	public String listSearchItem() throws Exception {
		write(viewParseService.getParseSearchItemsJSON(getUserId(), menuId,
				searchPanelId));
		return null;
	}

	/**
	 * 
	 * Description : 显示元素
	 * 
	 * @return String
	 * @throws Exception
	 * 
	 */
	public String listGridStructure() throws Exception {
		write(viewParseService.getParseGridsJSON(getUserId(), gridPanelId));
		return null;
	}

	/**
	 * 
	 * Description : 显示待审数据
	 * 
	 * @return String
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String listGridData1() throws Exception {
		CacheUtil.clearCacheByClass(AuditRecord.class);

		Map inputMap = ViewAction.requestParameterMap2Map(request);
		inputMap.put("user_id", this.getUserId());
		inputMap.put("audit_status", "wait");
		Map map = viewParseService.getParseGridDatas(getUserId(), gridPanelId,
				searchPanelId, inputMap);
		write(JSONObject.fromObject(map).toString());

		return null;
	}

	/**
	 * 
	 * Description : 显示已审数据
	 * 
	 * @return String
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String listGridData2() throws Exception {
		CacheUtil.clearCacheByClass(AuditRecord.class);

		Map inputMap = ViewAction.requestParameterMap2Map(request);
		int userId = this.getUserId();
		if (userId != 1)
			inputMap.put("user_id", userId);
		inputMap.put("audit_status", "pass,overrule");
		Map map = viewParseService.getParseGridDatas(getUserId(), gridPanelId,
				searchPanelId, inputMap);
		write(JSONObject.fromObject(map).toString());

		return null;
	}

	/**
	 * 
	 * Description : 显示审核流程数据
	 * 
	 * @return String
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String viewAuditFlow() throws Exception {
		String contentId = request.getParameter("content_id");
		String typeCode = request.getParameter("type_code");
		String start = request.getParameter(ViewConstant.JSON_PAGE_START);
		String limit = request.getParameter(ViewConstant.JSON_PAGE_LIMIT);
		int pageSize = limit != null ? Integer.parseInt(limit) : 10;
		int pageNo = start != null ? (Integer.parseInt(start) / pageSize + 1)
				: 1;

		List auditRecordList = auditRecordService.viewAuditFlow(contentId,
				typeCode, pageNo, pageSize).getResultList();
		List<Map> columnMaps = ViewUtil.Objects2ColumnMapsBy_C(auditRecordList);
		Map viewGridMap = viewParseService.getParseGridDatasFromList(
				getUserId(), gridPanelId, columnMaps, columnMaps.size());
		write(JSONObject.fromObject(viewGridMap).toString());

		return null;
	}

	/**
	 * 
	 * Description : 打开面板窗口
	 * 
	 * @return String
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String openPanelWindow() throws Exception {

		List<Map> mapList = ViewAction.getDataMap(request);
		Map inputMap = null;
		if (mapList.size() > 0) {
			inputMap = mapList.get(0);
		}
		Map map = viewParseService.getParseViewByEventId(getUserId(), menuId,
				viewEventId, inputMap);
		write(JSONObject.fromObject(map).toString());

		return null;

	}

	/**
	 * 
	 * Description : 审核通过 request{id:AuditRecord表pk, idea}
	 * 
	 */
	public void agree() throws Exception {
		boolean flag = false;

		String id = request.getParameter("id");
		String idea = request.getParameter("idea");

		AuditRecord auditRecord = auditRecordService.getByKey(new Long(id));
		if (auditRecord != null) {
			flag = true;
			if (idea != null && !idea.equals("")) {
				auditRecord.setIdea(idea);
			} else {
				auditRecord.setIdea("同意");
			}
			try {
				auditRecordService.agree(auditRecord);
			} catch (Exception e) {
				flag = false;
			}

		} else {
			flag = false;
		}

		// 记录日志
		createLog("agree", "AuditRecord title:" + auditRecord.getTitle());

		if (flag) {
			sendSuccessMessage("审核操作成功!");
		} else {
			sendFailureMessage("审核操作失败!");
		}
	}

	/**
	 * 
	 * Description : 批量通过 request{ids:id,id,id,...}
	 * 
	 */
	@SuppressWarnings("unchecked")
	public void batchAgree() throws Exception {
		boolean flag = false;

		List<Map> mapList = ViewAction.getDataMap(request);

		for (Map map : mapList) {
			AuditRecord auditRecord = auditRecordService.getByKey(new Long(map
					.get("id").toString()));
			if (auditRecord != null) {
				auditRecord.setIdea("同意");
				flag = true;
				try {
					auditRecordService.agree(auditRecord);
				} catch (Exception e) {
					flag = false;
				}

				// 记录日志
				createLog("batchAgree", "AuditRecord title:"
						+ auditRecord.getTitle());
			} else {
				flag = false;
				break;
			}
		}

		if (flag) {
			sendSuccessMessage("批量通过操作成功!");
		} else {
			sendFailureMessage("批量通过操作失败!");
		}
	}

	/**
	 * 
	 * Description : 审核驳回 request{id-AuditRecord表pk, idea}
	 * 
	 */
	public void refuse() throws Exception {
		boolean flag = false;

		String id = request.getParameter("id");
		String idea = request.getParameter("idea");

		AuditRecord auditRecord = auditRecordService.getByKey(new Long(id));
		if (auditRecord != null) {
			if (idea != null && !idea.equals("")) {
				auditRecord.setIdea(idea);
			} else {
				auditRecord.setIdea("不同意");
			}
			flag = true;
			try {
				auditRecordService.refuse(auditRecord);
			} catch (Exception e) {
				flag = false;
			}
		} else {
			flag = false;
		}

		// 记录日志
		createLog("refuse", "AuditRecord title:" + auditRecord.getTitle());

		if (flag) {
			sendSuccessMessage("审核操作成功!");
		} else {
			sendFailureMessage("审核操作失败!");
		}
	}

	/**
	 * 
	 * Description : 查看审核意见 request{id-AuditRecord表pk}
	 * 
	 */
	@SuppressWarnings("unchecked")
	public void viewIdea() throws Exception {
		String id = request.getParameter("id");
		Map result = new HashMap();
		if (id != null) {
			AuditRecord auditRecord = auditRecordService.getByKey(new Long(id));
			result.put("success", true);
			result.put("msg", "查看成功!");
			result.put("idea", auditRecord.getIdea());
		} else {
			result.put("failure", true);
			result.put("msg", "查看失败!");
			result.put("idea", "查看失败!");
		}
		write(JSONObject.fromObject(result).toString());
	}

	/*********************** 属性设置 ***********************/

	public void setMenuId(int menuId) {
		this.menuId = menuId;
	}

	public void setViewParseService(ViewParseService viewParseService) {
		this.viewParseService = viewParseService;
	}

	public void setViewEventId(String viewEventId) {
		this.viewEventId = viewEventId;
	}

	public void setGridPanelId(String gridPanelId) {
		this.gridPanelId = gridPanelId;
	}

	public void setSearchPanelId(String searchPanelId) {
		this.searchPanelId = searchPanelId;
	}

	public void setAuditRecordService(IAuditRecordService auditRecordService) {
		this.auditRecordService = auditRecordService;
	}

	public BusinessService getBusinessService() {
		return businessService;
	}

	public void setBusinessService(BusinessService businessService) {
		this.businessService = businessService;
	}

	public IAuditRecordService getAuditRecordService() {
		return auditRecordService;
	}
}
